from urllib import request, parse
import requests
from lxml import etree
import ssl

# 取消 https 代理验证
ssl._create_default_https_context = ssl._create_unverified_context


def cxysearch(self, outline, keyword, page, sort, pageSize):
    # 报头
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36'
    headers = {'User-Agent': user_agent}
    cxy_url = ""
    response = {}
    if outline == '1':
        cxy_url = "http://ciliquan.com/api/query"
    elif outline == '2':
        cxy_url = "https://cilixingapi.pw/searchapi"
        # cxy_url = "https://www.cat4.xyz/list"

    try:
        if outline == '1':
            response = requests.get(
                cxy_url + "?keyword=" + parse.quote(keyword) + "&page=" + page + "&sort=" + sort + "&pageSize=" + pageSize,
                headers=headers)
        elif outline == '2':
            response = requests.get(cxy_url + "?keyword=" + parse.quote(keyword) + "&p=" + page + "&m=" + sort, headers=headers)
        return response.json()
    except Exception:
        print(Exception)
        data = {"code": -1, "message": "error", "data": None}
        return data
